<?php
/**
 * Created by PhpStorm.
 * User: DD
 * Date: 2016/4/18
 * Time: 14:36
 */

namespace app\common\controller;


use app\admin\controller\Login;
use app\common\services\BaseService;
use app\common\services\LoginService;
use app\common\util\excel\SimpleExcel;
use app\common\util\Util;
use app\common\util\ValueBag;
use think\Controller;
use think\Response;

class Base extends Controller
{
    /**
     * @var BaseService
     */
    protected $_service;

    public function __construct()
    {
        parent::__construct();
        $this->_setService();
        if (empty($_GET['old'])) {
            if (!empty($_SERVER['HTTP_REFERER'])) {
                $prefer = $_SERVER['HTTP_REFERER'];
            } else {
                $prefer = $_SERVER['REQUEST_URI'];
            }
            if (strpos($prefer, '/old/') === false) {
                $this->view->theme('bootstrap');
            }
        }
    }

    protected function _getClassName()
    {
        return lcfirst(Util::getClassName($this));
    }

    protected function _setService()
    {
        $this->_service = new BaseService(Util::getModelName($this));
    }

    public function index()
    {
        return $this->fetch();
    }

    public function form()
    {
        $id = I('get.id', 0, 'intval');
        $isEditing = 0;
        if ($id) {
            $model = $this->_service->getModelInstance()->find($id);
            $isEditing = 1;
        } else {
            $model = [];
        }
        $this->assign('isEdit', $isEditing);
        $this->assign('m', ValueBag::wrap($model));
        return $this->fetch();
    }

    public function detail()
    {
        $id = I('get.id', 0, 'intval');
        $this->assign('m', ValueBag::wrap($this->_service->getModelInstance()->where(['id'=>$id])->find()));
        return $this->fetch();
    }

    public function returnJson($data)
    {
        Response::type('json');
        return $data;
    }

    public function jsonSuccess($info = '')
    {
        return $this->returnJson([
            'info' => $info,
            'status' => 1
        ]);
    }

    public function jsonFail($info = '')
    {
        return $this->returnJson([
            'info' => $info,
            'status' => 0
        ]);
    }

    public function all($sort = 'id', $order = 'desc')
    {
        return $this->returnJson($this->_service->all($sort, $order, I('filterRules', '', 'trim')));
    }

    public function pagination($page = 1, $rows = 10, $sort = 'id', $order = 'desc')
    {
        $data = $this->_service->pagination($page, $rows, $sort, $order, I('filterRules', '', 'trim'));
        return $this->returnJson([
            'total' => $data[0],
            'rows' => $data[1]
        ]);
    }

    public function saveModel()
    {
        $rt = $this->_service->saveModel($_REQUEST);
        if ($rt[0]) {
            return $this->jsonSuccess(empty($rt[1]) ? '' : $rt[1]);
        }
        return $this->jsonFail($rt[1]);
    }

    public function exportModel()
    {
        $data = json_decode(urldecode(I('post.exportParam')), true);
        if (!empty($data)) {
            $title = $data['title'] . date('Y-m-d');
            $rows = $this->_service->exportModel($data);
            $exportOptions = $data['exportOptions'];
            SimpleExcel::output($title, $rows, $exportOptions);
        }
        return '';
    }

    public function deleteModel()
    {
        $ids = $_REQUEST['ids'];
        if ($ids) {
            $rt = $this->_service->deleteModel($ids);
            if ($rt) {
                return $this->jsonSuccess();
            }
        }
        return $this->jsonFail();
    }

    public function getComboList()
    {
        return $this->returnJson($this->_service->getComboList());
    }

    public function commonData($data)
    {
        if (empty($data)) {
            return $this->jsonFail('');
        }
        if (!is_array($data)) {
            return $this->jsonFail($data);
        }
        if (isset($data[0]) && $data[0] == true) {
            return $this->jsonSuccess(isset($data[1]) ? $data[1] : '');
        }
        return $this->jsonFail(isset($data[1]) ? $data[1] : '');
    }

    public function editField()
    {
        $ids = I('post.ids', '', 'trim');
        if ($ids) {
            unset($_POST['ids']);
            $rt = $this->_service->editField($ids, $_POST);
            if ($rt) {
                return $this->jsonSuccess();
            }
        }
        return $this->jsonFail();
    }
}